#include "MacXml.h"

MacXml::MacXml()
{

}


QString MacXml::mac() const
{
    return m_mac;
}

QString MacXml::registrant() const
{
    return m_registrant;
}

QByteArray MacXml::registrationDateBCD() const
{
    return m_registrationDateBCD;
}


bool MacXml::parser()
{
    if(!this->check())
    {
        return false;
    }
    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root = doc.documentElement();
    QDomNode rootChild = root.firstChild();

    while (!rootChild.isNull())
    {
        if(rootChild.toElement().tagName() == "MAC")  //解析mac地址
        {
            m_mac = rootChild.toElement().text();
        }
        if(rootChild.toElement().tagName() == "Registrant")    //解析注册人
        {
            m_registrant = rootChild.toElement().text();
        }
        if(rootChild.toElement().tagName() == "Date")  //解析注册时间
        {
            QString registrationDate = rootChild.toElement().text();
            registrationDate.replace(QRegExp("\\-|\\:|\\ "),",").insert(2,",");
            QStringList timeList = registrationDate.split(",");
            for(int i=0; i<timeList.count(); i++)
            {
                quint8 temp = timeList.at(i).toInt()%10+timeList.at(i).toInt()/10*16;
                m_registrationDateBCD.append(temp);
            }
        }
        rootChild = rootChild.nextSibling();
    }

    return true;
}

bool MacXml::modify(const QString &mac,const QString &registrant,const QByteArray &registrationDateBCD)
{

    if(!this->clear())
    {
        return false;
    }

    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root=doc.documentElement();

    QDomText domText;
    QString  strTest;

    QDomElement macElement = doc.createElement("MAC");
    strTest = mac;
    domText = doc.createTextNode(strTest);
    macElement.appendChild(domText);
    root.appendChild(macElement);

    domText.clear();
    QDomElement registrantElement = doc.createElement("Registrant");
    strTest = registrant;
    domText = doc.createTextNode(strTest);
    registrantElement.appendChild(domText);
    root.appendChild(registrantElement);

    domText.clear();
    QDomElement registrationDateElement = doc.createElement("Date");

    QString registrationDate;
    registrationDate += QString::number(registrationDateBCD.at(0),16);
    registrationDate += QString::number(registrationDateBCD.at(1),16);
    registrationDate += "-";
    registrationDate += QString::number(registrationDateBCD.at(2),16);
    registrationDate += "-";
    registrationDate += QString::number(registrationDateBCD.at(3),16);
    registrationDate += " ";
    registrationDate += QString::number(registrationDateBCD.at(4),16);
    registrationDate += ":";
    registrationDate += QString::number(registrationDateBCD.at(5),16);
    registrationDate += ":";
    registrationDate += QString::number(registrationDateBCD.at(6),16);
    strTest = registrationDate;
    domText = doc.createTextNode(strTest);
    registrationDateElement.appendChild(domText);
    root.appendChild(registrationDateElement);

    this->m_mac = mac;
    this->m_registrant = registrant;
    this->m_registrationDateBCD = registrationDateBCD;

    file.open(QFile::WriteOnly|QFile::Truncate); //先读进来，再重写，如果不用truncate就是在后面追加内容，就无效了
    QTextStream outStream(&file);
    doc.save(outStream,4); //缩进4格
    file.flush();
    file.close();
    return true;
}


bool MacXml::clear()
{   
    if(!this->clearAllNode())
    {
        return false;
    }

    m_mac.clear();
    m_registrant.clear();
    m_registrationDateBCD.clear();

    return true;
}



